import { AvailableRenderersPlugin } from 'amis-editor';
// 初始化低代码设计器左侧面板
import { useLayoutEffect } from 'react';
import { LowCodeEditorPanel } from './lowcode';
// 初始化门户首页设计器左侧面板
import { PortalEditPanel } from './portal';

type ModuleType = 'portal' | 'lowcode';

const editPanels = {
  portal: PortalEditPanel,
  lowcode: LowCodeEditorPanel,
  __origin: AvailableRenderersPlugin.prototype.buildEditorPanel,
};

// 重写左边组件面板
function initBuildEditPanel(type?: ModuleType) {
  AvailableRenderersPlugin.prototype.buildEditorPanel = (type ? editPanels[type] : null) || editPanels.__origin;

  return () => {
    AvailableRenderersPlugin.prototype.buildEditorPanel = editPanels.__origin;
  };
}

// 启用自定义面板
export function useEditorPanel(type: ModuleType) {
  useLayoutEffect(() => initBuildEditPanel(type), [type]);
}
